package com.jsmy.softapp.util;

import android.Manifest;
import android.app.Activity;

import com.jsmy.softapp.R;
import com.jsmy.softapp.onListener.onUMShareActionListener;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import io.reactivex.functions.Consumer;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/27.
 * Time: 17:58.
 * To change for file | settings | file and code templates.
 */

public class ShareUtil {

    public static void shareWeb(final Activity context, final String url, final String title, final String des, final UMShareListener onUMShareActionListener) {
        RxPermissions rxPermissions = new RxPermissions(context);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            UMWeb web = new UMWeb(url);
                            web.setTitle(title);
                            web.setDescription(des);
                            web.setThumb(new UMImage(context, R.drawable.icon_audio));
                            ShareAction shareAction = new ShareAction(context);
                            shareAction.withMedia(web);
                            ShareActionUtils.show(context, shareAction, onUMShareActionListener);
                        } else {
                            ToastUtil.showLong(context, "缺少权限，请授予SD卡的使用权限！");
                        }
                    }
                });

    }

    public void shareImg(Activity context, String url, String title, String des, onUMShareActionListener onUMShareActionListener) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);
        web.setDescription(des);
        web.setThumb(new UMImage(context, R.drawable.icon_audio));
        ShareAction shareAction = new ShareAction(context);
        shareAction.withMedia(web);
        ShareActionUtils.show(context, shareAction, onUMShareActionListener);
    }

    public void shareText(Activity context, String url, String title, String des, onUMShareActionListener onUMShareActionListener) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);
        web.setDescription(des);
        web.setThumb(new UMImage(context, R.drawable.icon_audio));
        ShareAction shareAction = new ShareAction(context);
        shareAction.withMedia(web);
        ShareActionUtils.show(context, shareAction, onUMShareActionListener);
    }
}
